package org.jeecg.modules.cw.controller;

import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.*;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.modules.cw.entity.*;
import org.jeecg.modules.cw.service.ICwApprovalMessageService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.vo.LoginUser;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.cw.vo.CwLxrygzjbxmxbTjPage;
import org.jeecg.modules.cw.service.ICwLxrygzjbxmxbTjService;
import org.jeecg.modules.cw.service.ICwLxrygzjbxmxbService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 离休人员工资及保险明细提交表
 * @Author: jeecg-boot
 * @Date:   2025-09-05
 * @Version: V1.0
 */
@Api(tags="离休人员工资及保险明细提交表")
@RestController
@RequestMapping("/cw/cwLxrygzjbxmxbTj")
@Slf4j
public class CwLxrygzjbxmxbTjController {
	@Autowired
	private ICwLxrygzjbxmxbTjService cwLxrygzjbxmxbTjService;
	@Autowired
	private ICwLxrygzjbxmxbService cwLxrygzjbxmxbService;
	 @Autowired
	 private ICwApprovalMessageService cwApprovalMessageService;
	 @Autowired
	 private ISysDepartService sysDepartService;
	 @Autowired
	 private ISysBaseAPI iSysBaseAPI;
	/**
	 * 分页列表查询
	 *
	 * @param cwLxrygzjbxmxbTj
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "离休人员工资及保险明细提交表-分页列表查询")
	@ApiOperation(value="离休人员工资及保险明细提交表-分页列表查询", notes="离休人员工资及保险明细提交表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<CwLxrygzjbxmxbTj>> queryPageList(CwLxrygzjbxmxbTj cwLxrygzjbxmxbTj,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<CwLxrygzjbxmxbTj> queryWrapper = QueryGenerator.initQueryWrapper(cwLxrygzjbxmxbTj, req.getParameterMap());
		Page<CwLxrygzjbxmxbTj> page = new Page<CwLxrygzjbxmxbTj>(pageNo, pageSize);
		// 获取登录用户信息
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		if (user == null || StringUtils.isEmpty(user.getId())) {
			return Result.error("未获取到登录用户信息，查询失败");
		}
		String currentUserId = user.getId();

		// 获取用户所属部门名称（departName）
		String departIdsByOrgCode = iSysBaseAPI.getDepartIdsByOrgCode(user.getOrgCode());
		SysDepart depart = sysDepartService.getById(departIdsByOrgCode);
		if (depart == null || StringUtils.isEmpty(depart.getDepartName())) {
			return Result.error("未获取到用户所属部门信息，查询失败");
		}
		String departName = depart.getDepartName(); // 用户所属部门名称

		// 定义特权用户ID
		String admin1 = "1985179654538330114";  // 第一个特权用户（拥有待审批权限）
		String admin2 = "1985180799365218306";  // 第二个特权用户
		String admin3 = "1985180919854989314";  // 第三个特权用户

		// 定义需要过滤的部门（机关、鉴定站）
		List<String> allowedDepartments = Arrays.asList("机关", "鉴定站");
		if (admin1.equals(currentUserId)) {
			// 第一个特权用户：
			// 1. 待审批状态仅查询“机关”和“鉴定站”部门
			// 2. 其他状态（通过、驳回、第一人审批同意）无部门限制
			queryWrapper.and(wrapper -> wrapper
					// 待审批 + 限定部门
					.or().eq("state", "待审批").in("unit", allowedDepartments)
					// 其他状态无部门限制
					.or().in("state", "通过", "驳回", "第一人审批同意")
			);
		} else if (admin2.equals(currentUserId) || admin3.equals(currentUserId)) {
			// 第二、三个特权用户：仅可查询通过、驳回
			queryWrapper.in("state", "通过", "驳回","第一人审批同意");
		} else {
			// 非特权用户：仅查询unit等于其所属部门名称的数据
			queryWrapper.eq("unit", departName);
		}
		IPage<CwLxrygzjbxmxbTj> pageList = cwLxrygzjbxmxbTjService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	 /**
	  * 审批
	  * @param id 记录ID
	  * @return 处理结果
	  */
	 @AutoLog(value = "审批-审批")
	 @ApiOperation(value="审批-审批", notes="审批-审批")
	 @RequestMapping(value = "/approval", method = {RequestMethod.PUT, RequestMethod.POST})
	 public Result<String> approval(@RequestParam String id,
									@RequestParam String approvalResult,
									@RequestParam String approvalRemark) {
		 //获取当前用户名
		 LoginUser user= (LoginUser)SecurityUtils.getSubject().getPrincipal();
		 String userId=user.getId();
		 CwLxrygzjbxmxbTj byId = cwLxrygzjbxmxbTjService.getById(id);
		 CwApprovalMessage cwApprovalMessage = new CwApprovalMessage();
		 cwApprovalMessage.setMainId(byId.getId());
		 cwApprovalMessage.setApprovalMessage(approvalRemark);
		 cwApprovalMessage.setApprovalTime(new Date());
		 cwApprovalMessage.setApprover(userId);
		 cwApprovalMessage.setState(approvalResult);
		 cwApprovalMessage.setSpr(user.getRealname());
		 cwApprovalMessageService.save(cwApprovalMessage);
		 if ("通过".equals(approvalResult)) {
			 if ("1985179754371153921".equals(userId)||"1985179977394880513".equals(userId)||
					 "1985180115760775170".equals(userId)||"1985180266818633730".equals(userId)
					 ||"1985180455415513090".equals(userId)){
				 byId.setState("第一人审批同意");
			 }else {
				 byId.setState(approvalResult);
			 }
		 }else {
			 byId.setState(approvalResult);
		 }
		 byId.setApprovalTime(new Date());
		 cwLxrygzjbxmxbTjService.updateById(byId);
		 return Result.OK("提交成功");
	 }

	 /**
	  * 提交处理
	  * @param id 记录ID
	  * @return 处理结果
	  */
	 @AutoLog(value = "提交-提交")
	 @ApiOperation(value="提交-提交", notes="提交-提交")
	 @RequestMapping(value = "/tj", method = {RequestMethod.PUT, RequestMethod.POST})
	 public Result<String> tj(@RequestParam String id) {
		 //获取当前用户名
		 LoginUser user= (LoginUser)SecurityUtils.getSubject().getPrincipal();
		 String userName=user.getRealname();
		 log.info(id);
		 CwLxrygzjbxmxbTj byId = cwLxrygzjbxmxbTjService.getById(id);
		 byId.setState("待审批");
		 cwLxrygzjbxmxbTjService.updateById(byId);
		 return Result.OK("提交成功");
	 }
	/**
	 *   添加
	 *
	 * @param cwLxrygzjbxmxbTjPage
	 * @return
	 */
	@AutoLog(value = "离休人员工资及保险明细提交表-添加")
	@ApiOperation(value="离休人员工资及保险明细提交表-添加", notes="离休人员工资及保险明细提交表-添加")
//@RequiresPermissions("org.jeecg.modules:cw_lxrygzjbxmxb_tj:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody CwLxrygzjbxmxbTjPage cwLxrygzjbxmxbTjPage) {
		CwLxrygzjbxmxbTj cwLxrygzjbxmxbTj = new CwLxrygzjbxmxbTj();
		BeanUtils.copyProperties(cwLxrygzjbxmxbTjPage, cwLxrygzjbxmxbTj);
		//获取当前用户名
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String departIdsByOrgCode = iSysBaseAPI.getDepartIdsByOrgCode(user.getOrgCode());
		SysDepart byId = sysDepartService.getById(departIdsByOrgCode);
		String departName = byId.getDepartName();
		cwLxrygzjbxmxbTj.setUnit(departName);
		cwLxrygzjbxmxbTj.setMainId(user.getId());
		Double a = 0.0;
		Double b = 0.0;

		for (CwLxrygzjbxmxb cwHealthForm : cwLxrygzjbxmxbTjPage.getCwLxrygzjbxmxbList()) {
			// 为每个可能为null的字段设置默认值0.0（三目运算符实现）
			Double jblxf = cwHealthForm.getJblxf() != null ? cwHealthForm.getJblxf() : 0.0;
			Double nqzj = cwHealthForm.getNqzj() != null ? cwHealthForm.getNqzj() : 0.0;
			Double nhzj = cwHealthForm.getNhzj() != null ? cwHealthForm.getNhzj() : 0.0;
			Double npzjdshf = cwHealthForm.getNpzjdshf() != null ? cwHealthForm.getNpzjdshf() : 0.0;
			Double dsznjf = cwHealthForm.getDsznjf() != null ? cwHealthForm.getDsznjf() : 0.0;
			Double lxshbt = cwHealthForm.getLxshbt() != null ? cwHealthForm.getLxshbt() : 0.0;
			Double jf = cwHealthForm.getJf() != null ? cwHealthForm.getJf() : 0.0;
			Double jlm = cwHealthForm.getJlm() != null ? cwHealthForm.getJlm() : 0.0;
			Double tsgwjt = cwHealthForm.getTsgwjt() != null ? cwHealthForm.getTsgwjt() : 0.0;
			Double wsf = cwHealthForm.getWsf() != null ? cwHealthForm.getWsf() : 0.0;
			Double tgjf = cwHealthForm.getTgjf() != null ? cwHealthForm.getTgjf() : 0.0;
			Double lmyyjt = cwHealthForm.getLmyyjt() != null ? cwHealthForm.getLmyyjt() : 0.0;
			Double nzjdshbt = cwHealthForm.getNzjdshbt() != null ? cwHealthForm.getNzjdshbt() : 0.0;
			Double nursingFee = cwHealthForm.getNursingFee() != null ? cwHealthForm.getNursingFee() : 0.0;
			Double zjjblxf = cwHealthForm.getZjjblxf() != null ? cwHealthForm.getZjjblxf() : 0.0;
			Double njfgz = cwHealthForm.getNjfgz() != null ? cwHealthForm.getNjfgz() : 0.0;

			// 计算 住房提租补贴（使用默认值后的字段计算）
			// 先计算a的基数和（减少重复计算）
			Double aBaseSum = jblxf + nqzj + nhzj + npzjdshf + dsznjf + lxshbt + jf + jlm + tsgwjt + wsf + tgjf;
			a = aBaseSum * 0.13;

			// 计算b的总和（包含a和其他字段）
			b = aBaseSum + lmyyjt + nzjdshbt + nursingFee + a + zjjblxf;
			b = new BigDecimal(aBaseSum + lmyyjt + nzjdshbt + nursingFee + a + zjjblxf).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			// 原有精度处理逻辑不变
			Double zftzbt = new BigDecimal(a).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			Double ylbxjsn = new BigDecimal((b - a) * 12 + njfgz).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			Double ylbx = new BigDecimal(ylbxjsn * 0.72).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			Double yhj = new BigDecimal(b).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			Double nhj = new BigDecimal(b * 12 + njfgz).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

			cwHealthForm.setZfjzbt(zftzbt);
			cwHealthForm.setYyffgzhj(b);
			cwHealthForm.setNyfgzhj(nhj);
			cwHealthForm.setYlbxjs(ylbxjsn);
			cwHealthForm.setYlbx(ylbx);
			cwHealthForm.setYjsbfhzj(ylbxjsn);
			cwHealthForm.setUnit(cwLxrygzjbxmxbTj.getUnit());
			cwHealthForm.setYear(cwLxrygzjbxmxbTjPage.getYear());
			cwHealthForm.setUserId(user.getId());
		}
		cwLxrygzjbxmxbTj.setState("待提交");
		cwLxrygzjbxmxbTjService.saveMain(cwLxrygzjbxmxbTj, cwLxrygzjbxmxbTjPage.getCwLxrygzjbxmxbList());
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param cwLxrygzjbxmxbTjPage
	 * @return
	 */
	@AutoLog(value = "离休人员工资及保险明细提交表-编辑")
	@ApiOperation(value="离休人员工资及保险明细提交表-编辑", notes="离休人员工资及保险明细提交表-编辑")
    //@RequiresPermissions("org.jeecg.modules:cw_lxrygzjbxmxb_tj:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody CwLxrygzjbxmxbTjPage cwLxrygzjbxmxbTjPage) {
		CwLxrygzjbxmxbTj cwLxrygzjbxmxbTj = new CwLxrygzjbxmxbTj();
		BeanUtils.copyProperties(cwLxrygzjbxmxbTjPage, cwLxrygzjbxmxbTj);
		Double a = 0.0;
		Double b = 0.0;
		CwLxrygzjbxmxbTj cwLxrygzjbxmxbTjEntity = cwLxrygzjbxmxbTjService.getById(cwLxrygzjbxmxbTj.getId());
		if(cwLxrygzjbxmxbTjEntity==null) {
			return Result.error("未找到对应数据");
		}
		LoginUser user = (LoginUser)SecurityUtils.getSubject().getPrincipal();
		String id = user.getId();
		for (CwLxrygzjbxmxb cwHealthForm : cwLxrygzjbxmxbTjPage.getCwLxrygzjbxmxbList()) {
			// 为每个可能为null的字段设置默认值0.0（三目运算符实现）
			Double jblxf = cwHealthForm.getJblxf() != null ? cwHealthForm.getJblxf() : 0.0;
			Double nqzj = cwHealthForm.getNqzj() != null ? cwHealthForm.getNqzj() : 0.0;
			Double nhzj = cwHealthForm.getNhzj() != null ? cwHealthForm.getNhzj() : 0.0;
			Double npzjdshf = cwHealthForm.getNpzjdshf() != null ? cwHealthForm.getNpzjdshf() : 0.0;
			Double dsznjf = cwHealthForm.getDsznjf() != null ? cwHealthForm.getDsznjf() : 0.0;
			Double lxshbt = cwHealthForm.getLxshbt() != null ? cwHealthForm.getLxshbt() : 0.0;
			Double jf = cwHealthForm.getJf() != null ? cwHealthForm.getJf() : 0.0;
			Double jlm = cwHealthForm.getJlm() != null ? cwHealthForm.getJlm() : 0.0;
			Double tsgwjt = cwHealthForm.getTsgwjt() != null ? cwHealthForm.getTsgwjt() : 0.0;
			Double wsf = cwHealthForm.getWsf() != null ? cwHealthForm.getWsf() : 0.0;
			Double tgjf = cwHealthForm.getTgjf() != null ? cwHealthForm.getTgjf() : 0.0;
			Double lmyyjt = cwHealthForm.getLmyyjt() != null ? cwHealthForm.getLmyyjt() : 0.0;
			Double nzjdshbt = cwHealthForm.getNzjdshbt() != null ? cwHealthForm.getNzjdshbt() : 0.0;
			Double nursingFee = cwHealthForm.getNursingFee() != null ? cwHealthForm.getNursingFee() : 0.0;
			Double zjjblxf = cwHealthForm.getZjjblxf() != null ? cwHealthForm.getZjjblxf() : 0.0;
			Double njfgz = cwHealthForm.getNjfgz() != null ? cwHealthForm.getNjfgz() : 0.0;

			// 计算 住房提租补贴（使用默认值后的字段计算）
			// 先计算a的基数和（减少重复计算）
			Double aBaseSum = jblxf + nqzj + nhzj + npzjdshf + dsznjf + lxshbt + jf + jlm + tsgwjt + wsf + tgjf;
			a = aBaseSum * 0.13;

			// 计算b的总和（包含a和其他字段）
//			b = aBaseSum + lmyyjt + nzjdshbt + nursingFee + a + zjjblxf;
			b = new BigDecimal(aBaseSum + lmyyjt + nzjdshbt + nursingFee + a + zjjblxf).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			// 原有精度处理逻辑不变
			Double zftzbt = new BigDecimal(a).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			Double ylbxjsn = new BigDecimal((b - a) * 12 + njfgz).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			Double ylbx = new BigDecimal(ylbxjsn * 0.72).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			Double yhj = new BigDecimal(b).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			Double nhj = new BigDecimal(b * 12 + njfgz).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			System.out.println( "月应发合计"+b+ ""+b*12+"     dddd:"+njfgz   +"he:"+b*12+njfgz);
			cwHealthForm.setZfjzbt(zftzbt);
			cwHealthForm.setYyffgzhj(b);
			cwHealthForm.setNyfgzhj(nhj);
			cwHealthForm.setYlbxjs(ylbxjsn);
			cwHealthForm.setYlbx(ylbx);
			cwHealthForm.setYjsbfhzj(ylbxjsn);
			cwHealthForm.setUnit(cwLxrygzjbxmxbTj.getUnit());
			cwHealthForm.setYear(cwLxrygzjbxmxbTjPage.getYear());
			cwHealthForm.setUserId(user.getId());
			cwHealthForm.setYear(cwLxrygzjbxmxbTjPage.getYear());

		}
		cwLxrygzjbxmxbTjService.updateMain(cwLxrygzjbxmxbTj, cwLxrygzjbxmxbTjPage.getCwLxrygzjbxmxbList());
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "离休人员工资及保险明细提交表-通过id删除")
	@ApiOperation(value="离休人员工资及保险明细提交表-通过id删除", notes="离休人员工资及保险明细提交表-通过id删除")
    //@RequiresPermissions("org.jeecg.modules:cw_lxrygzjbxmxb_tj:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		cwLxrygzjbxmxbTjService.delMain(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "离休人员工资及保险明细提交表-批量删除")
	@ApiOperation(value="离休人员工资及保险明细提交表-批量删除", notes="离休人员工资及保险明细提交表-批量删除")
    //@RequiresPermissions("org.jeecg.modules:cw_lxrygzjbxmxb_tj:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.cwLxrygzjbxmxbTjService.delBatchMain(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "离休人员工资及保险明细提交表-通过id查询")
	@ApiOperation(value="离休人员工资及保险明细提交表-通过id查询", notes="离休人员工资及保险明细提交表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<CwLxrygzjbxmxbTj> queryById(@RequestParam(name="id",required=true) String id) {
		CwLxrygzjbxmxbTj cwLxrygzjbxmxbTj = cwLxrygzjbxmxbTjService.getById(id);
		if(cwLxrygzjbxmxbTj==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(cwLxrygzjbxmxbTj);

	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "离休人员工资及保险明细表-通过主表ID查询")
	@ApiOperation(value="离休人员工资及保险明细表-通过主表ID查询", notes="离休人员工资及保险明细表-通过主表ID查询")
	@GetMapping(value = "/queryCwLxrygzjbxmxbByMainId")
	public Result<IPage<CwLxrygzjbxmxb>> queryCwLxrygzjbxmxbListByMainId(@RequestParam(name="id",required=true) String id) {
		List<CwLxrygzjbxmxb> cwLxrygzjbxmxbList = cwLxrygzjbxmxbService.selectByMainId(id);
		IPage <CwLxrygzjbxmxb> page = new Page<>();
		page.setRecords(cwLxrygzjbxmxbList);
		page.setTotal(cwLxrygzjbxmxbList.size());
		return Result.OK(page);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param cwLxrygzjbxmxbTj
    */
    //@RequiresPermissions("org.jeecg.modules:cw_lxrygzjbxmxb_tj:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CwLxrygzjbxmxbTj cwLxrygzjbxmxbTj) {
      // Step.1 组装查询条件查询数据
      QueryWrapper<CwLxrygzjbxmxbTj> queryWrapper = QueryGenerator.initQueryWrapper(cwLxrygzjbxmxbTj, request.getParameterMap());
      LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

     //配置选中数据查询条件
      String selections = request.getParameter("selections");
      if(oConvertUtils.isNotEmpty(selections)) {
           List<String> selectionList = Arrays.asList(selections.split(","));
           queryWrapper.in("id",selectionList);
      }
      //Step.2 获取导出数据
      List<CwLxrygzjbxmxbTj>  cwLxrygzjbxmxbTjList = cwLxrygzjbxmxbTjService.list(queryWrapper);

      // Step.3 组装pageList
      List<CwLxrygzjbxmxbTjPage> pageList = new ArrayList<CwLxrygzjbxmxbTjPage>();
      for (CwLxrygzjbxmxbTj main : cwLxrygzjbxmxbTjList) {
          CwLxrygzjbxmxbTjPage vo = new CwLxrygzjbxmxbTjPage();
          BeanUtils.copyProperties(main, vo);
          List<CwLxrygzjbxmxb> cwLxrygzjbxmxbList = cwLxrygzjbxmxbService.selectByMainId(main.getId());
          vo.setCwLxrygzjbxmxbList(cwLxrygzjbxmxbList);
          pageList.add(vo);
      }

      // Step.4 AutoPoi 导出Excel
      ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
      mv.addObject(NormalExcelConstants.FILE_NAME, "离休人员工资及保险明细提交表列表");
      mv.addObject(NormalExcelConstants.CLASS, CwLxrygzjbxmxbTjPage.class);
      mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("离休人员工资及保险明细提交表数据", "导出人:"+sysUser.getRealname(), "离休人员工资及保险明细提交表"));
      mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
      return mv;
    }

    /**
    * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("org.jeecg.modules:cw_lxrygzjbxmxb_tj:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
      Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
      for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
          // 获取上传文件对象
          MultipartFile file = entity.getValue();
          ImportParams params = new ImportParams();
          params.setTitleRows(2);
          params.setHeadRows(1);
          params.setNeedSave(true);
          try {
              List<CwLxrygzjbxmxbTjPage> list = ExcelImportUtil.importExcel(file.getInputStream(), CwLxrygzjbxmxbTjPage.class, params);
              for (CwLxrygzjbxmxbTjPage page : list) {
                  CwLxrygzjbxmxbTj po = new CwLxrygzjbxmxbTj();
                  BeanUtils.copyProperties(page, po);
                  cwLxrygzjbxmxbTjService.saveMain(po, page.getCwLxrygzjbxmxbList());
              }
              return Result.OK("文件导入成功！数据行数:" + list.size());
          } catch (Exception e) {
              log.error(e.getMessage(),e);
              return Result.error("文件导入失败:"+e.getMessage());
          } finally {
              try {
                  file.getInputStream().close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      return Result.OK("文件导入失败！");
    }

}
